﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Common;
using System.Threading;
using System.Net;
using System.Net.Sockets;

namespace ZhccPua
{
    public partial class ZhccMainForm : Form
    {
        Thread ConnectServer;

        NetworkStream myNetWorkStream;
        TcpClient myTcpClient;
        public IPAddress myAddress
        {
            get
            {
                if (textBox1.Text == string.Empty)
                {
                    return IPAddress.Any;
                }
                return IPAddress.Parse(textBox1.Text);
            }
        }
        IPEndPoint myEndPoint;

        public ZhccMainForm()
        {
            InitializeComponent();
            ConnectServer = new Thread(new ThreadStart(Connect));
            textBox1.Text = "192.168.1.128";
        }

        void Connect()
        {
           
            myTcpClient = new TcpClient();
            myEndPoint = new IPEndPoint(myAddress, 8848);
            try
            {
                myTcpClient.Connect(myEndPoint);

                if (myTcpClient.Connected)
                {
                    myNetWorkStream = myTcpClient.GetStream();
                    StopTimer();
                    Thread messageThread = new Thread(new ThreadStart(StartMessageProcess));
                    messageThread.IsBackground = true;
                    messageThread.Start();
                }
                
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void StopTimer()
        {
            ConnectTimer.Stop();
        }

        private void StartMessageProcess()
        {
            try
            {
                while (true)
                {
                

                    byte[] mybuffer = new byte[1024];
                    myNetWorkStream.Read(mybuffer,0,mybuffer.Length);

                    AppendLog(Encoding.Default.GetString(mybuffer));
            
                }
            }
            catch (Exception ex)
            {
                AppendLog(ex.Message);
                Thread.CurrentThread.Abort();
            }
        }

        private void AppendLog(string p)
        {
            rtbResultCode.AppendText(String.Format("{0}---{1}\r\n", p, DateTime.Now.ToShortTimeString()));
        }

        public void SendMessage(string msg)
        {
            //Log Send Message TODO
            var mybuffer = new byte[1024];
            mybuffer = Encoding.Default.GetBytes(msg);
            myNetWorkStream.Write(mybuffer, 0, mybuffer.Length);
        }
             

        private void button1_Click(object sender, EventArgs e)
        {

            bool isok = false;

            rtbResultCode.AppendText(String.Format("{0}\r\n{1}", Security.DecryptDES(tbSecCode.Text, tbKey.Text, out isok), isok));
        }

        private void button2_Click(object sender, EventArgs e)
        {
            ConnectServer.Start();

            //if (Security.CheckIsRegisterUser())
            //{
            //    MessageBox.Show("isok");
            //}
        }

        private void ZhccMainForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            ConnectServer.Abort();

        }

        private void ConnectTimer_Tick(object sender, EventArgs e)
        {
            Connect();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            SendMessage(tbMsg.Text);
        }
    }
}
